package com.gitee.zhanyao.cloud.mq.consumer.rocketmq;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 生产者配置
 * @author chendm
 * @create 2021-03-09 15:40
 */
@Slf4j
@ToString
@Configuration
public class MQProducerConfigure {

    @Autowired
    private MQProducerProperties mqProducerProperties;

    @Bean
    public DefaultMQProducer defaultMQProducer()throws MQClientException{
        log.info("defaultProducer正在创建--------------------------------------");
        //1. 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer(mqProducerProperties.getGroupName());
        //2. 设置NameServer的地址
        producer.setNamesrvAddr(mqProducerProperties.getNamesrvAddr());
        //3. 关闭vip通道
        producer.setVipChannelEnabled(false);
        //4. 设置消息最大值
        producer.setMaxMessageSize(mqProducerProperties.getMaxMessageSize());
        //5. 设置消息发送超时时间
        producer.setSendMsgTimeout(mqProducerProperties.getSendMsgTimeOut());
        //6. 设置失败重试次数
        producer.setRetryTimesWhenSendAsyncFailed(mqProducerProperties.getRetryTimesWhenSendFailed());
        producer.start();
        log.info("rocketmq producer server 开启成功----------------------------");
        return producer;
    }
}
